package edu.spec.student.service.impl;

import edu.spec.student.dao.iface.IMentalDao;
import edu.spec.student.dao.iface.IRubricDao;
import edu.spec.student.model.mental.Mental;
import edu.spec.student.model.mental.Rubric;
import edu.spec.student.service.iface.IRubricService;
import edu.spec.basic.util.FileTool;
import edu.spec.basic.util.XlsTools;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Collection;
import java.util.List;

/**
 * Created by MadFrog on 2017/6/20.
 */
@Service
public class RubricService implements IRubricService {

    @Inject
    private IMentalDao mentalDao;
    @Inject
    private IRubricDao rubricDao;

    @Override
    public void add(Rubric r) {
        rubricDao.add(r);
    }

    @Override
    public List<Rubric> listRubricByMental(Integer mentalId) {
        return rubricDao.listRubricByMental(mentalId);
    }

    @Override
    public boolean importIn(Integer mentalId, MultipartFile file, HttpServletRequest req) {
        String fullStorePath = FileTool.saveTempFile(file, req);
        File importFile = new File(fullStorePath);
        Mental m = mentalDao.load(mentalId);
        Collection col = XlsTools.importExcel(importFile, Rubric.class, "");
        for(Object o : col){
            Rubric r = (Rubric)o;
            r.setMental(m);
            this.add(r);
        }
        importFile.delete();
        return true;
    }
}
